package com.lab.webSocket.service.impl;

import com.lab.webSocket.dto.UserDto;
import com.lab.webSocket.dto.UserInfoDto;
import com.lab.webSocket.model.FriendsListEntity;
import com.lab.webSocket.repository.FriendListRepository;
import com.lab.webSocket.repository.UserRepository;
import com.lab.webSocket.service.AddUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/9/7.
 */
@Service
public class AddUserServiceImpl implements AddUserService {
    @Resource
    private FriendListRepository friendListRepository;
    @Resource
    private UserRepository userRepository;

    public Integer addFriendByUserAccount(String friendAccount, String userAccount) throws Exception {
        FriendsListEntity friendsListEntity = new FriendsListEntity(friendAccount, userAccount);
        return friendListRepository.save(friendsListEntity).getFriendListId();
    }

    public List<UserInfoDto> findFriendsByInfo(String info) throws Exception {
        return userRepository.findFriendsByInfo(info);
    }

    @Transactional
    public Integer updateUserInfo(UserDto userDto) throws Exception {
        return userRepository.updateUserInfo(
                userDto.getUserId(),
                userDto.getUserNickname(),
                userDto.getUserSex(),
                userDto.getUserAge(),
                userDto.getUserIcon(),
                userDto.getUserProfile());
    }

    public UserDto getUserInfo(String userAccount) throws Exception {
        return userRepository.getUserInfo(userAccount);
    }
}
